package com.ewzj.bigdata.mvp.presenter;

import android.app.Activity;
import android.app.Application;
import android.text.TextUtils;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.ShareDataBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ArticleBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResCollectSuccess;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResNewsData;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.ArticleDetailContract;
import com.jess.arms.utils.ArmsUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

import static com.umeng.socialize.utils.DeviceConfig.context;


@ActivityScope
public class ArticleDetailPresenter extends BasePresenter<ArticleDetailContract.Model, ArticleDetailContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Activity context;
    private int page=1;
    @Inject
    public ArticleDetailPresenter (ArticleDetailContract.Model model, ArticleDetailContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        this.context= (Activity) mRootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void loadArticleInfo(String id){
        mModel.getArticleDetail(id).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ArticleBean>(mErrorHandler) {
                    @Override
                    public void onNext(ArticleBean articleBean) {
                        mRootView.showArticleView(articleBean);
                    }
                });
    }

    public void loadGuessLike(String id,String cateid){
        mModel.getGuessLike(id,cateid).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<NewBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<NewBean> newBeans) {
                        mRootView.showGuessLikeRv(newBeans);
                    }
                });
    }


    public void ShareWeb(ShareDataBean infodata, SHARE_MEDIA share ) {
        UMWeb web = new UMWeb(infodata.getThree_url());
        if (!TextUtils.isEmpty(infodata.getThumb())){
            UMImage thumb = new UMImage(context, infodata.getThumb());
            web.setThumb(thumb);
        }else{
            UMImage thumb = new UMImage(context, R.drawable.share_default);
            web.setThumb(thumb);
        }
        if (TextUtils.isEmpty(infodata.getSummary())){
            web.setDescription("觅窝.新媒体");
        }else{
            web.setDescription(infodata.getSummary());
        }
        web.setTitle(infodata.getTitle());
        new ShareAction(context).withMedia(web).setPlatform(share).setCallback(AppConstants.umShareListener).share();
    }

    public void loadCollectData(){
        page=1;
        mModel.getCollects(page).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<CollectBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<CollectBean> collectBeans) {
                        page++;
                        mRootView.showDoCollectPopWindow(collectBeans,false);
                    }
                });
    }

    public void loadMoreCollectData(){
        mModel.getCollects(page).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<CollectBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<CollectBean> collectBeans) {
                        page++;
                        mRootView.notifyCollectDataChange(collectBeans);
                        if (collectBeans.size()<AppConstants.PAGE_NUM){
                            mRootView.obtainAdapter().loadMoreEnd();
                        }
                    }
                });
    }

    public void doCollect(String id,String groupid){
        mModel.doCollect(id,groupid)
                .compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<ResCollectSuccess>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<ResCollectSuccess> resCollectSuccessBaseData) {
                        ArmsUtils.snackbarText("添加收藏成功");
                    }
                });
    }
}
